diff --git a/.gitignore b/.gitignore index 7335a1c..9b4fb41 100644 --- a/.gitignore +++ b/.gitignore @@ -133,4 +133,3 @@ example prompts graphify-out -.modules-dev diff --git a/lib/craco-fix-plugin.js b/lib/craco-fix-plugin.js index 23210a4..aff8306 100644 --- a/lib/craco-fix-plugin.js +++ b/lib/craco-fix-plugin.js @@ -1,6 +1,7 @@ const {when} = require("@kne/craco"); const env = require("./env"); +// alias 到绝对路径时,即便落在 node_modules 下,ModuleScopePlugin 仍按 src 外拦截 const allowReadmeOutsideSrc = (webpackConfig) => { const plugins = webpackConfig.resolve && webpackConfig.resolve.plugins; if (!Array.isArray(plugins)) { @@ -36,11 +37,10 @@ module.exports = { } }); } - webpackConfig.resolve = webpackConfig.resolve || {}; webpackConfig.resolve.alias = Object.assign({}, webpackConfig.resolve.alias, { readme: env.readmeDir }); - webpackConfig.resolve.fallback = Object.assign({}, webpackConfig.resolve.fallback, { + webpackConfig.resolve.fallback = { "path": false, "util": false, "url": false, @@ -51,7 +51,7 @@ module.exports = { "querystring": false, "zlib": false, "fs": false - }); + }; allowReadmeOutsideSrc(webpackConfig); return webpackConfig; } diff --git a/lib/env.js b/lib/env.js index cfc9195..90df7e2 100644 --- a/lib/env.js +++ b/lib/env.js @@ -3,9 +3,7 @@ const ensureSlash = require('@kne/ensure-slash'); const template = require('lodash/template'); const appDir = process.cwd(); -// 虚拟 readme 不能放在 node_modules 下:workplace 软链 node_modules 时 webpack -// 会 realpath 到 SOURCE,找不到虚拟文件。 -const modulesDevDir = path.resolve(appDir, '.modules-dev'); +const modulesDevDir = path.resolve(appDir, 'node_modules/.modules-dev'); const readmeDir = path.resolve(modulesDevDir, 'readme'); const manifestPath = path.resolve(readmeDir, 'modules.js'); const readmeIndexPath = path.resolve(readmeDir, 'index.js'); diff --git a/lib/readme-webpack-plugin.js b/lib/readme-webpack-plugin.js index e487d56..b08dbe1 100644 --- a/lib/readme-webpack-plugin.js +++ b/lib/readme-webpack-plugin.js @@ -9,7 +9,7 @@ const buildComponentDocs = require('./build-component-docs'); const ReadmeWebpackPlugin_MODULE_NAMES = 'ReadmeWebpackPlugin_MODULE_NAMES'; -/** 将虚拟 readme 同步到磁盘(内容不变则不写,避免 watch 循环)。workplace 软链 node_modules 时供 source-map-loader realpath。 */ +/** 将虚拟 readme 同步到磁盘(内容不变则不写,避免 watch 循环)。 */ const writeReadmeFilesIfChanged = (files) => { fs.ensureDirSync(env.readmeDir); Object.entries(files).forEach(([file, content]) => { diff --git a/package.json b/package.json index c8987d3..a0294ab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kne/modules-dev", - "version": "2.4.11", + "version": "2.4.12", "description": "用于辅助在项目内启动一个规范化组件开发的环境", "publishConfig": { "access": "public", diff --git a/test/craco-fix-plugin.test.js b/test/craco-fix-plugin.test.js index c5e88a7..d79f63f 100644 --- a/test/craco-fix-plugin.test.js +++ b/test/craco-fix-plugin.test.js @@ -23,10 +23,21 @@ describe('craco-fix-plugin concatenateModules', () => { }); describe('craco-fix-plugin readme alias', () => { - it('应将 readme 别名到 .modules-dev/readme', () => { + it('应将 readme 别名到 node_modules/.modules-dev/readme', () => { const webpackConfig = createConfig(true); plugin.overrideWebpackConfig({ webpackConfig, context: { env: 'development' } }); expect(webpackConfig.resolve.alias.readme).to.equal(env.readmeDir); }); + + it('应把 readme 目录加入 ModuleScopePlugin allowedPaths', () => { + const webpackConfig = createConfig(true); + webpackConfig.resolve.plugins = [{ + constructor: { name: 'ModuleScopePlugin' }, + allowedFiles: new Set(), + allowedPaths: [] + }]; + plugin.overrideWebpackConfig({ webpackConfig, context: { env: 'development' } }); + expect(webpackConfig.resolve.plugins[0].allowedPaths).to.include(env.readmeDir); + }); }); diff --git a/test/env.test.js b/test/env.test.js index b13a077..01eb84a 100644 --- a/test/env.test.js +++ b/test/env.test.js @@ -3,11 +3,10 @@ const { expect } = require('chai'); const env = require('../lib/env'); describe('env readme paths', () => { - it('虚拟 readme 应落在 .modules-dev 下而不是 node_modules', () => { - expect(env.readmeDir).to.equal(path.resolve(process.cwd(), '.modules-dev/readme')); + it('虚拟 readme 应落在 node_modules/.modules-dev 下', () => { + expect(env.readmeDir).to.equal(path.resolve(process.cwd(), 'node_modules/.modules-dev/readme')); expect(env.readmeIndexPath).to.equal(path.resolve(env.readmeDir, 'index.js')); expect(env.manifestPath).to.equal(path.resolve(env.readmeDir, 'modules.js')); - expect(env.manifestPath.includes(`${path.sep}node_modules${path.sep}`)).to.equal(false); - expect(env.readmeIndexPath.includes(`${path.sep}node_modules${path.sep}`)).to.equal(false); + expect(env.modulesDevDir).to.equal(path.resolve(process.cwd(), 'node_modules/.modules-dev')); }); });