Laravel 5.2升5.6:从Gulp+Elixir迁移至Webpack+Mix的技术咨询
System.import When Migrating from Laravel Elixir to Mix (5.2 → 5.6) Hey there! I’ve walked through this exact upgrade path a few times, so let’s get that System.import code sorted out for your Laravel 5.6 project.
First, a quick context check: Laravel 5.2’s Elixir relied on SystemJS for module loading, but Laravel 5.6’s Mix uses Webpack, which has its own (more modern) approach to dynamic imports. That System.import syntax won’t work out of the box with Webpack, but fixing it is straightforward.
Here’s your step-by-step fix:
Replace
System.importwith Webpack’s native dynamicimport()
Webpack supports the ES6+ dynamic import syntax directly, so swap out your existing code:// Old (SystemJS syntax) // System.import('rim/bootstrap.js').then(function(Rim) { Rim.... // New (Webpack-compatible ES6 syntax) import('rim/bootstrap.js').then((Rim) => { // Your existing Rim initialization code here Rim.... });Laravel Mix’s default Webpack/Babel setup already supports this syntax, but if you run into compilation errors, double-check that your project has the
@babel/plugin-syntax-dynamic-importplugin installed (Mix usually includes this by default).Verify your module path resolution
Make sure Webpack can locate yourrim/bootstrap.jsfile. If this file lives inresources/assets/js/rim/bootstrap.js, the path should work as-is. If it’s in a different directory, you can add an alias to yourwebpack.mix.jsto simplify imports:const path = require('path'); mix.webpackConfig({ resolve: { alias: { 'rim': path.resolve(__dirname, 'path/to/your/rim/directory/') } } });This lets you keep using
import('rim/bootstrap.js')instead of typing out the full relative path every time.Confirm your
Rimmodule’s export format
Ensurerim/bootstrap.jsis exporting theRimobject correctly. If it uses CommonJS (e.g.,module.exports = Rim) or ES modules (e.g.,export default Rim), Webpack will hand it off to your.then()callback without issues. If the original code was attachingRimto thewindowobject, you might need to adjust the bootstrap file to explicitly export it instead.Test and debug
Runnpm run devto compile your assets, then check your browser console for any module-not-found or syntax errors. If you hit issues, start by verifying the file path and export format—those are the most common gotchas here.
And one last note: You can go ahead and remove any old SystemJS configuration from your former Gulp/Elixir setup—Webpack handles all module loading now, so those settings are no longer needed.
内容的提问来源于stack exchange,提问作者James

