Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/craco-fix-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,44 @@ const isolateAppCache = (webpackConfig) => {
});
};

const NODE_MODULES_MJS = /\.mjs$/;
const ESM_CONDITION_NAMES = ['import', 'module', '...'];

const isNodeModulesMjsAutoRule = (rule) => {
if (!rule || rule.type !== 'javascript/auto') {
return false;
}
return Boolean(rule.test) && String(rule.test) === String(NODE_MODULES_MJS);
};

// webpack 5.107+ 解析 dual package 时可能先命中 exports.require(CJS),
// CRA 再按 ESM named export 校验 → `X is not exported from 'motion-utils'`。
// 优先 import/module,并把 node_modules 的 .mjs 当普通 JS(fullySpecified: false)。
const preferEsmFromNodeModules = (webpackConfig) => {
webpackConfig.module = webpackConfig.module || {};
webpackConfig.module.rules = webpackConfig.module.rules || [];
if (!webpackConfig.module.rules.some(isNodeModulesMjsAutoRule)) {
webpackConfig.module.rules.unshift({
test: NODE_MODULES_MJS,
include: /node_modules/,
type: 'javascript/auto',
resolve: {
fullySpecified: false
}
});
}

webpackConfig.resolve = webpackConfig.resolve || {};
const current = webpackConfig.resolve.conditionNames;
const alreadyPreferred = Array.isArray(current)
&& current[0] === 'import'
&& current[1] === 'module'
&& current[2] === '...';
if (!alreadyPreferred) {
webpackConfig.resolve.conditionNames = ESM_CONDITION_NAMES.slice();
}
};

// alias 到绝对路径时,即便落在 node_modules 下,ModuleScopePlugin 仍按 src 外拦截
const allowReadmeOutsideSrc = (webpackConfig) => {
const plugins = webpackConfig.resolve && webpackConfig.resolve.plugins;
Expand Down Expand Up @@ -97,6 +135,7 @@ module.exports = {
"zlib": false,
"fs": false
};
preferEsmFromNodeModules(webpackConfig);
allowReadmeOutsideSrc(webpackConfig);
isolateAppCache(webpackConfig);
return webpackConfig;
Expand Down
2 changes: 1 addition & 1 deletion lib/readme-webpack-plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const fs = require('fs-extra');
const VirtualModulesPlugin = require('webpack-virtual-modules');
const VirtualModulesPlugin = require('./webpack-virtual-modules');
const virtualModules = new VirtualModulesPlugin();
const env = require('./env');
const { getModuleList } = require('./utils');
Expand Down
21 changes: 21 additions & 0 deletions lib/webpack-virtual-modules/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 SysGears

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions lib/webpack-virtual-modules/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# webpack-virtual-modules (vendored)

Copied from [webpack-virtual-modules@0.6.2](https://github.com/sysgears/webpack-virtual-modules) (MIT).

Used by `readme-webpack-plugin.js` to inject virtual readme/manifest modules during webpack compile.

**Why vendor:** `@kne/modules-dev` previously depended on `webpack-virtual-modules@^0.5.0`. That release throws `Cannot read properties of null (reading 'fileWatchers')` when `writeModule` runs while `WatchFileSystem.watcher` is still `null` (common on newer Node/webpack). `0.6.2` guards this; keeping the implementation in-repo avoids relying on the external package resolution.
15 changes: 15 additions & 0 deletions lib/webpack-virtual-modules/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Compiler } from 'webpack';
declare const ALL = "all";
declare const STATIC = "static";
declare const DYNAMIC = "dynamic";
declare type AvailableModules = typeof ALL | typeof STATIC | typeof DYNAMIC;
declare class VirtualModulesPlugin {
private _staticModules;
private _compiler;
private _watcher;
constructor(modules?: Record<string, string>);
getModuleList(filter?: AvailableModules): {};
writeModule(filePath: string, contents: string): void;
apply(compiler: Compiler): void;
}
export = VirtualModulesPlugin;
Loading