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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,3 @@ example

prompts
graphify-out
.modules-dev
6 changes: 3 additions & 3 deletions lib/craco-fix-plugin.js
Original file line number Diff line number Diff line change
@@ -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)) {
Expand Down Expand Up @@ -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,
Expand All @@ -51,7 +51,7 @@ module.exports = {
"querystring": false,
"zlib": false,
"fs": false
});
};
allowReadmeOutsideSrc(webpackConfig);
return webpackConfig;
}
Expand Down
4 changes: 1 addition & 3 deletions lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion lib/readme-webpack-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kne/modules-dev",
"version": "2.4.11",
"version": "2.4.12",
"description": "用于辅助在项目内启动一个规范化组件开发的环境",
"publishConfig": {
"access": "public",
Expand Down
13 changes: 12 additions & 1 deletion test/craco-fix-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

7 changes: 3 additions & 4 deletions test/env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
});